[<?
  // ob_start("ob_gzhandler");

  require_once('../db.php');
  require_once('../util/misc.php');
  db_connect();

  $build_clusters = true;

  $bboxs = $_GET["bbox"];
  $w = intval($_GET["w"]);
  $h = intval($_GET["h"]);
  $z = intval($_GET["z"]);
  $bbox = false;
  if ($z > 5) {
    $bbox = split(',', $bboxs, 4);
    for ($i=0; $i<4; $i++)
      $bbox[$i] = doubleval($bbox[$i]);
  }

  $query = 'SELECT id_TPT AS id, unix_timestamp(date_time_TPT) AS tm, longitude_TPT AS lng, latitude_TPT AS lat, test_tpt AS test';
  $query .= ', description_TPT as descr, user_TPT AS uid, username AS un, name AS fn';
  $query .= ', length_TPT as length, width_TPT as width, height_TPT as height, dispersed_TPT as isdisp, bag_eq_TPT as bageq';
  $query .= ', photo1_TPT as photo1, photo2_TPT as photo2, exclude_TPT as excluded, cleaned_tpt as cleaned';
  $query .= ', bad_road_TPT as dif, bad_road_rain_TPT as difr';
  $query .= ', perc_non_reciclable_TPT as perc0, perc_glass_TPT as perc1, perc_metal_TPT as perc2, perc_plastic_TPT as perc3';
  $query .= ', perc_paper_TPT as perc4, perc_rubber_TPT as perc5, perc_big_volume_TPT as perc6, perc_construction_TPT as perc7';
  $query .= ', verified_tpt as ver';
  $query .= ' FROM v_tpt_non_ver';
  if ($bbox)
    $query .= " WHERE Intersects(location_TPT, GeomFromText('POLYGON((".
              $bbox[0]." ".$bbox[1].",".$bbox[2]." ".$bbox[1].",".$bbox[2]." ".$bbox[3].",".$bbox[0]." ".$bbox[3].",".$bbox[0]." ".$bbox[1].
              "))'))";
  $query .= ' ORDER BY id_TPT';
  $result = mysql_query($query) or die("ERROR PERFORMING QUERY '".$query."': ".mysql_error());

  $first = true;
  while ($row = mysql_fetch_assoc($result)) {
    if ($first)
      $first = false;
    else
      echo ",\n";
    echo "{id:".$row["id"].",type:1,lat:".$row["lat"].",lng:".$row["lng"].",tm:".$row["tm"].",uid:".$row["uid"];
    echo ",dl:".$row["length"].",dw:".$row["width"].",dh:".$row["height"];
    if ($row["isdisp"] === 'Y') {
      echo ",disp:true";
      if (!is_null($row["bageq"]))
        echo ",bageq:".$row["bageq"];
      else
        echo ",bageq:".($row["length"]*$row["width"]*$row["height"])*10/3;
    }
    else {
      echo ",disp:false";
      echo ",bageq:".($row["length"]*$row["width"]*$row["height"])*10/3;
    }
    if ($row["dif"] === 'Y')
      echo ",dif:true";
    else
      echo ",dif:false";
    if ($row["difr"] === 'Y')
      echo ",difr:true";
    else
      echo ",difr:false";
    echo ",perc:[";
    for ($i=0; $i<8; $i++) {
      if ($i > 0)
        echo ",";
      echo $row["perc".$i];
    }
    echo "]";

    echo ",ph:[";
    $np = 0;
    if (!is_null($row["photo1"])) {
      echo "'".jsenc($row["photo1"])."'";
      $np++;
    }
    if (!is_null($row["photo2"])) {
      if ($np > 0)
        echo ",";
      echo "'".jsenc($row["photo2"])."'";
    }
    echo "]";

    if (!is_null($row["excluded"]) && $row["excluded"] !== 'N' && strlen($row["excluded"])!==0)
      $ic = 1; // excluded
    elseif ($row["cleaned"] === 'Y')
      $ic = 2; // cleaned
    elseif ($row["ver"] !== 'Y')
      $ic = 3; // non-verified
    else
      $ic = 0; // non-cleaned
    echo ",ic:".$ic;

    echo ",un:'".jsenc($row["un"])."',fn:'".$row["fn"]."',desc:'".jsenc($row["descr"])."'}";
  }

  mysql_free_result($result);

  $query = 'SELECT id_fls AS id, unix_timestamp(time_date_fls) AS tm, longitude_fls AS lng, latitude_fls AS lat';
  $query .= ', name_fls AS name, description_fls as descr';
  $query .= ' FROM ldi_flagstation_fls ORDER BY id_fls';
  $result = mysql_query($query) or die("ERROR PERFORMING QUERY '".$query."': ".mysql_error());

  while ($row = mysql_fetch_assoc($result)) {
    if ($first)
      $first = false;
    else
      echo ",\n";
    echo "{id:".$row["id"].",type:3,lat:".$row["lat"].",lng:".$row["lng"].",tm:".$row["tm"];
    echo ",name:'".jsenc($row["name"])."',desc:'".jsenc($row["descr"])."'";
    echo ",ic:4";
    echo "}";
  }

  mysql_free_result($result);
?>
]
